package cn.iocoder.yudao.module.dine.desk.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.module.api.shop.category.eums.ShopCategoryCodeEnum;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.api.shop.info.message.ShopAuditPassMessage;
import cn.iocoder.yudao.module.dine.desk.dao.DeskCategoryMapper;
import cn.iocoder.yudao.module.dine.desk.pojo.po.DeskCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 餐桌分类 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class DeskCategoryConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ShopAuditPassMessage message) {
        ShopInfoDTO shopInfo = message.getShopInfo();
        log.info("[dine/desk-category][onMessage][店铺审核通过事件({})]", shopInfo.getName());
        if (Objects.equals(shopInfo.getCategoryCode(), ShopCategoryCodeEnum.FOOD.getCode())) {
            DeskCategory entity = new DeskCategory();
            entity.setShopId(shopInfo.getId());
            entity.setName("双人桌");
            entity.setPeople(2);
            SpringUtil.getBean(DeskCategoryMapper.class).insert(entity);
        }
    }

}
